package com.swh.controller;

import com.swh.entity.Result;
import com.swh.entity.SysUser;
import com.swh.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/10/23 18:43
 * @Version: 1.0
 */
@Controller
@RequestMapping("/main")
public class MainController {

    @Autowired
    private SysUserService sysUserService;


    @RequestMapping("/navbar")
    public String navbar() {
        return "/common/navbar";
    }

    @RequestMapping("/sidebar")
    public String sidebar() {
        return "/common/sidebar";
    }

    @ResponseBody
    @RequestMapping("/login")
    public Result login(String username, String password, HttpServletRequest request, HttpSession session, HttpServletResponse response) throws IOException {
//        String message = "用户名或密码错误";
//        SysUser loginUser = new SysUser();
//        loginUser.setUsername(username);
//        loginUser.setPassword(EncryptUtils.MD5_HEX(EncryptUtils.MD5_HEX(password) + username));
//        loginUser = sysUserService.selectOne(loginUser);
//        System.out.println(loginUser);
//        if (loginUser != null) {
//            //正确登录用户
//            //存session
//            session.setAttribute("loginUser", loginUser);
//
//            String ip = request.getRemoteHost();
//            loginUser.setLoginIp(ip);
//            loginUser.setLoginDate(new Date());
//            sysUserService.update(loginUser);
//
//            //清空密码
//            loginUser.setPassword(null);
//
//
//            return new Result(true, "登录成功", loginUser);
//        } else {
//            return new Result(false, message, null);
//        }
        SysUser loginUser = sysUserService.findByName(username);
        Md5Hash md5Hash = new Md5Hash(password,loginUser.getUsername(),2);
//        System.out.println(md5Hash);
//        System.out.println(loginUser.getName());
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, String.valueOf(md5Hash));
//        System.out.println("pass:"+password);


        try {
            subject.login(token);
        } catch (AuthenticationException e) {
//            e.printStackTrace();
            return new Result(false, "用户名或密码错误", null);
        }


        String ip = request.getRemoteHost();
        loginUser.setLoginIp(ip);
        loginUser.setLoginDate(new Date());
        sysUserService.update(loginUser);
        //存session
        session.setAttribute("loginUser", loginUser);



        return new Result(true, "登录成功", loginUser);
    }

    @ResponseBody
    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
//        session.invalidate();
        return new Result(true, "退出成功", null);
    }

}
